package utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import domain.User;
import domain.Wgroup;

public class TreeJsonUtil {
	public static List<Map<String, Object>> getTreeJson(Map<String, List<User>> data, List<Wgroup> order) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		
		if (data == null || order == null) {
			return null;
		}
		
		int root_id = 1;
		for (Wgroup key : order) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", root_id);
			map.put("text", key.getGroupName());
			map.put("groupId", key.getGroupId());
			List<Map<String, String>> children = getTreeUserJson(data.get(key.getGroupName()), root_id);
			if (children != null) {
				map.put("children", children);
			}
			result.add(map);
			root_id++;
		}
		
		return result;
	}
	
	public static List<Map<String, String>> getTreeUserJson(List<User> users, int root_id) {
		if (users == null || users.size() == 0) {
			return null;
		}
		
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		
		int id = 1;
		
		for (User u : users) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", root_id + "" + id);
			map.put("text", u.getUserName());
			map.put("userId", u.getUserId().toString());
			result.add(map);
			id++;
		}
		
		return result;
	}
}
